package mysql

import (
	"gitee.com/captials-team/ubdframe/src/domain/configstc"
	"gitee.com/captials-team/ubdframe/src/domain/interfaces"
	"gitee.com/captials-team/ubdframe/src/domain/models"
	mysqlClients "gitee.com/captials-team/ubdframe/src/infrastructure/clients/mysql"
	"gorm.io/gorm"
)

func NewOrganizeDao(conf configstc.DBConfig) *OrganizeDao {
	return &OrganizeDao{
		GormDBFacade: NewGormDBFacade[models.Organize](mysqlClients.NewGormDB(conf)),
		conf:         conf,
	}
}

type OrganizeDao struct {
	*GormDBFacade[models.Organize]
	conf configstc.DBConfig
}

func (dao *OrganizeDao) Delete(id int64) (int64, error) {
	ret := dao.db.Where("id=?", id).Where("id>0").Delete(new(models.Organize))

	if ret.Error != nil {
		return 0, ret.Error
	}
	return ret.RowsAffected, nil
}

func (dao *OrganizeDao) DeleteByPid(pid int64) (int64, error) {
	ret := dao.db.Where("pid=?", pid).Where("id>0").Delete(new(models.Organize))

	if ret.Error != nil {
		return 0, ret.Error
	}
	return ret.RowsAffected, nil
}

func (dao *OrganizeDao) ResetStatus(id int64, status int) (int64, error) {
	ret := dao.db.Model(new(models.Organize)).Where("id=?", id).Update("status", status)
	if ret.Error != nil {
		return 0, ret.Error
	}
	return ret.RowsAffected, nil
}

func (dao *OrganizeDao) Use(db *gorm.DB) interfaces.ItfOrganize {
	return &OrganizeDao{
		GormDBFacade: dao.GormDBFacade.New(db),
		conf:         dao.conf,
	}
}

func (dao *OrganizeDao) Migrate() (int64, error) {
	return 1, dao.db.AutoMigrate(new(models.Organize), new(models.OrganizeExtra))
}

func (dao *OrganizeDao) DB() *gorm.DB {
	return dao.db
}
